<?php

namespace app\admin\controller\data;

use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class User extends Backend
{

    /**
     * User模型对象
     * @var \app\admin\model\data\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\data\User;

    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        $ids = $this->request->param('ids','');
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as &$item){
                $f_id = Db::name('data_table') ->where('id',$item['d_id'])->value('f_id');
                $floor = Db::name('floor') ->where('id',$f_id)->field('name,u_id')->find();
                $unit = Db::name('unit') ->where('id',$floor['u_id'])->field('name,b_id')->find();
                $building = Db::name('building') ->where('id',$unit['b_id'])->value('name');
                $item['house_name'] =$building . $unit['name']. $floor['name'];
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        $this->assignconfig('data_id',$ids);
        return $this->view->fetch();
    }

}
